<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构 -->
      <el-card class="tree-card87 ">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <!-- 放置弹层组件 -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '../../api/departments'
import { tranListToTreeData } from '../../utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: { TreeTools, AddDept },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      company: {},
      showDialog: false,
      node: null,
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构数据列表
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      // console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '') // 需要将其转化成树形结构
      this.loading = false
    },
    // 点击添加子组件后通知父组件弹出dialog，并存储被点击节点的节点数据
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    // 点击编辑子组件后通知父组件弹出dialog，并存储被点击节点的数据
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true
      this.node = node
      // 利用ref注册组件，直接调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }

}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
